<!DOCTYPE HTML>
<html lang="en">
<head>
<title>Source code</title>
<link rel="stylesheet" type="text/css" href="../../../../../stylesheet.css" title="Style">
</head>
<body>
<main role="main">
<div class="sourceContainer">
<pre><span class="sourceLineNo">001</span><a id="line.1">/*</a>
<span class="sourceLineNo">002</span><a id="line.2"> * Licensed to the Apache Software Foundation (ASF) under one or more</a>
<span class="sourceLineNo">003</span><a id="line.3"> * contributor license agreements.  See the NOTICE file distributed with</a>
<span class="sourceLineNo">004</span><a id="line.4"> * this work for additional information regarding copyright ownership.</a>
<span class="sourceLineNo">005</span><a id="line.5"> * The ASF licenses this file to You under the Apache License, Version 2.0</a>
<span class="sourceLineNo">006</span><a id="line.6"> * (the "License"); you may not use this file except in compliance with</a>
<span class="sourceLineNo">007</span><a id="line.7"> * the License.  You may obtain a copy of the License at</a>
<span class="sourceLineNo">008</span><a id="line.8"> *</a>
<span class="sourceLineNo">009</span><a id="line.9"> *      http://www.apache.org/licenses/LICENSE-2.0</a>
<span class="sourceLineNo">010</span><a id="line.10"> *</a>
<span class="sourceLineNo">011</span><a id="line.11"> * Unless required by applicable law or agreed to in writing, software</a>
<span class="sourceLineNo">012</span><a id="line.12"> * distributed under the License is distributed on an "AS IS" BASIS,</a>
<span class="sourceLineNo">013</span><a id="line.13"> * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</a>
<span class="sourceLineNo">014</span><a id="line.14"> * See the License for the specific language governing permissions and</a>
<span class="sourceLineNo">015</span><a id="line.15"> * limitations under the License.</a>
<span class="sourceLineNo">016</span><a id="line.16"> */</a>
<span class="sourceLineNo">017</span><a id="line.17">package org.apache.commons.dbutils;</a>
<span class="sourceLineNo">018</span><a id="line.18"></a>
<span class="sourceLineNo">019</span><a id="line.19">import static java.sql.DriverManager.registerDriver;</a>
<span class="sourceLineNo">020</span><a id="line.20"></a>
<span class="sourceLineNo">021</span><a id="line.21">import java.io.PrintWriter;</a>
<span class="sourceLineNo">022</span><a id="line.22">import java.lang.reflect.Constructor;</a>
<span class="sourceLineNo">023</span><a id="line.23">import java.lang.reflect.InvocationTargetException;</a>
<span class="sourceLineNo">024</span><a id="line.24">import java.lang.reflect.Method;</a>
<span class="sourceLineNo">025</span><a id="line.25">import java.sql.Connection;</a>
<span class="sourceLineNo">026</span><a id="line.26">import java.sql.Driver;</a>
<span class="sourceLineNo">027</span><a id="line.27">import java.sql.DriverPropertyInfo;</a>
<span class="sourceLineNo">028</span><a id="line.28">import java.sql.ResultSet;</a>
<span class="sourceLineNo">029</span><a id="line.29">import java.sql.SQLException;</a>
<span class="sourceLineNo">030</span><a id="line.30">import java.sql.SQLFeatureNotSupportedException;</a>
<span class="sourceLineNo">031</span><a id="line.31">import java.sql.Statement;</a>
<span class="sourceLineNo">032</span><a id="line.32">import java.util.logging.Logger;</a>
<span class="sourceLineNo">033</span><a id="line.33">import java.util.Properties;</a>
<span class="sourceLineNo">034</span><a id="line.34"></a>
<span class="sourceLineNo">035</span><a id="line.35">/**</a>
<span class="sourceLineNo">036</span><a id="line.36"> * A collection of JDBC helper methods.  This class is thread safe.</a>
<span class="sourceLineNo">037</span><a id="line.37"> */</a>
<span class="sourceLineNo">038</span><a id="line.38">public final class DbUtils {</a>
<span class="sourceLineNo">039</span><a id="line.39"></a>
<span class="sourceLineNo">040</span><a id="line.40">    /**</a>
<span class="sourceLineNo">041</span><a id="line.41">     * Default constructor.</a>
<span class="sourceLineNo">042</span><a id="line.42">     *</a>
<span class="sourceLineNo">043</span><a id="line.43">     * Utility classes should not have a public or default constructor,</a>
<span class="sourceLineNo">044</span><a id="line.44">     * but this one preserves retro-compatibility.</a>
<span class="sourceLineNo">045</span><a id="line.45">     *</a>
<span class="sourceLineNo">046</span><a id="line.46">     * @since 1.4</a>
<span class="sourceLineNo">047</span><a id="line.47">     */</a>
<span class="sourceLineNo">048</span><a id="line.48">    public DbUtils() {</a>
<span class="sourceLineNo">049</span><a id="line.49">        // do nothing</a>
<span class="sourceLineNo">050</span><a id="line.50">    }</a>
<span class="sourceLineNo">051</span><a id="line.51"></a>
<span class="sourceLineNo">052</span><a id="line.52">    /**</a>
<span class="sourceLineNo">053</span><a id="line.53">     * Close a {@code Connection}, avoid closing if null.</a>
<span class="sourceLineNo">054</span><a id="line.54">     *</a>
<span class="sourceLineNo">055</span><a id="line.55">     * @param conn Connection to close.</a>
<span class="sourceLineNo">056</span><a id="line.56">     * @throws SQLException if a database access error occurs</a>
<span class="sourceLineNo">057</span><a id="line.57">     */</a>
<span class="sourceLineNo">058</span><a id="line.58">    public static void close(final Connection conn) throws SQLException {</a>
<span class="sourceLineNo">059</span><a id="line.59">        if (conn != null) {</a>
<span class="sourceLineNo">060</span><a id="line.60">            conn.close();</a>
<span class="sourceLineNo">061</span><a id="line.61">        }</a>
<span class="sourceLineNo">062</span><a id="line.62">    }</a>
<span class="sourceLineNo">063</span><a id="line.63"></a>
<span class="sourceLineNo">064</span><a id="line.64">    /**</a>
<span class="sourceLineNo">065</span><a id="line.65">     * Close a {@code ResultSet}, avoid closing if null.</a>
<span class="sourceLineNo">066</span><a id="line.66">     *</a>
<span class="sourceLineNo">067</span><a id="line.67">     * @param rs ResultSet to close.</a>
<span class="sourceLineNo">068</span><a id="line.68">     * @throws SQLException if a database access error occurs</a>
<span class="sourceLineNo">069</span><a id="line.69">     */</a>
<span class="sourceLineNo">070</span><a id="line.70">    public static void close(final ResultSet rs) throws SQLException {</a>
<span class="sourceLineNo">071</span><a id="line.71">        if (rs != null) {</a>
<span class="sourceLineNo">072</span><a id="line.72">            rs.close();</a>
<span class="sourceLineNo">073</span><a id="line.73">        }</a>
<span class="sourceLineNo">074</span><a id="line.74">    }</a>
<span class="sourceLineNo">075</span><a id="line.75"></a>
<span class="sourceLineNo">076</span><a id="line.76">    /**</a>
<span class="sourceLineNo">077</span><a id="line.77">     * Close a {@code Statement}, avoid closing if null.</a>
<span class="sourceLineNo">078</span><a id="line.78">     *</a>
<span class="sourceLineNo">079</span><a id="line.79">     * @param stmt Statement to close.</a>
<span class="sourceLineNo">080</span><a id="line.80">     * @throws SQLException if a database access error occurs</a>
<span class="sourceLineNo">081</span><a id="line.81">     */</a>
<span class="sourceLineNo">082</span><a id="line.82">    public static void close(final Statement stmt) throws SQLException {</a>
<span class="sourceLineNo">083</span><a id="line.83">        if (stmt != null) {</a>
<span class="sourceLineNo">084</span><a id="line.84">            stmt.close();</a>
<span class="sourceLineNo">085</span><a id="line.85">        }</a>
<span class="sourceLineNo">086</span><a id="line.86">    }</a>
<span class="sourceLineNo">087</span><a id="line.87"></a>
<span class="sourceLineNo">088</span><a id="line.88">    /**</a>
<span class="sourceLineNo">089</span><a id="line.89">     * Close a {@code Connection}, avoid closing if null and hide</a>
<span class="sourceLineNo">090</span><a id="line.90">     * any SQLExceptions that occur.</a>
<span class="sourceLineNo">091</span><a id="line.91">     *</a>
<span class="sourceLineNo">092</span><a id="line.92">     * @param conn Connection to close.</a>
<span class="sourceLineNo">093</span><a id="line.93">     */</a>
<span class="sourceLineNo">094</span><a id="line.94">    public static void closeQuietly(final Connection conn) {</a>
<span class="sourceLineNo">095</span><a id="line.95">        try {</a>
<span class="sourceLineNo">096</span><a id="line.96">            close(conn);</a>
<span class="sourceLineNo">097</span><a id="line.97">        } catch (final SQLException e) { // NOPMD</a>
<span class="sourceLineNo">098</span><a id="line.98">            // quiet</a>
<span class="sourceLineNo">099</span><a id="line.99">        }</a>
<span class="sourceLineNo">100</span><a id="line.100">    }</a>
<span class="sourceLineNo">101</span><a id="line.101"></a>
<span class="sourceLineNo">102</span><a id="line.102">    /**</a>
<span class="sourceLineNo">103</span><a id="line.103">     * Close a {@code Connection}, {@code Statement} and</a>
<span class="sourceLineNo">104</span><a id="line.104">     * {@code ResultSet}.  Avoid closing if null and hide any</a>
<span class="sourceLineNo">105</span><a id="line.105">     * SQLExceptions that occur.</a>
<span class="sourceLineNo">106</span><a id="line.106">     *</a>
<span class="sourceLineNo">107</span><a id="line.107">     * @param conn Connection to close.</a>
<span class="sourceLineNo">108</span><a id="line.108">     * @param stmt Statement to close.</a>
<span class="sourceLineNo">109</span><a id="line.109">     * @param rs ResultSet to close.</a>
<span class="sourceLineNo">110</span><a id="line.110">     */</a>
<span class="sourceLineNo">111</span><a id="line.111">    public static void closeQuietly(final Connection conn, final Statement stmt,</a>
<span class="sourceLineNo">112</span><a id="line.112">            final ResultSet rs) {</a>
<span class="sourceLineNo">113</span><a id="line.113"></a>
<span class="sourceLineNo">114</span><a id="line.114">        try {</a>
<span class="sourceLineNo">115</span><a id="line.115">            closeQuietly(rs);</a>
<span class="sourceLineNo">116</span><a id="line.116">        } finally {</a>
<span class="sourceLineNo">117</span><a id="line.117">            try {</a>
<span class="sourceLineNo">118</span><a id="line.118">                closeQuietly(stmt);</a>
<span class="sourceLineNo">119</span><a id="line.119">            } finally {</a>
<span class="sourceLineNo">120</span><a id="line.120">                closeQuietly(conn);</a>
<span class="sourceLineNo">121</span><a id="line.121">            }</a>
<span class="sourceLineNo">122</span><a id="line.122">        }</a>
<span class="sourceLineNo">123</span><a id="line.123"></a>
<span class="sourceLineNo">124</span><a id="line.124">    }</a>
<span class="sourceLineNo">125</span><a id="line.125"></a>
<span class="sourceLineNo">126</span><a id="line.126">    /**</a>
<span class="sourceLineNo">127</span><a id="line.127">     * Close a {@code ResultSet}, avoid closing if null and hide any</a>
<span class="sourceLineNo">128</span><a id="line.128">     * SQLExceptions that occur.</a>
<span class="sourceLineNo">129</span><a id="line.129">     *</a>
<span class="sourceLineNo">130</span><a id="line.130">     * @param rs ResultSet to close.</a>
<span class="sourceLineNo">131</span><a id="line.131">     */</a>
<span class="sourceLineNo">132</span><a id="line.132">    public static void closeQuietly(final ResultSet rs) {</a>
<span class="sourceLineNo">133</span><a id="line.133">        try {</a>
<span class="sourceLineNo">134</span><a id="line.134">            close(rs);</a>
<span class="sourceLineNo">135</span><a id="line.135">        } catch (final SQLException e) { // NOPMD</a>
<span class="sourceLineNo">136</span><a id="line.136">            // quiet</a>
<span class="sourceLineNo">137</span><a id="line.137">        }</a>
<span class="sourceLineNo">138</span><a id="line.138">    }</a>
<span class="sourceLineNo">139</span><a id="line.139"></a>
<span class="sourceLineNo">140</span><a id="line.140">    /**</a>
<span class="sourceLineNo">141</span><a id="line.141">     * Close a {@code Statement}, avoid closing if null and hide</a>
<span class="sourceLineNo">142</span><a id="line.142">     * any SQLExceptions that occur.</a>
<span class="sourceLineNo">143</span><a id="line.143">     *</a>
<span class="sourceLineNo">144</span><a id="line.144">     * @param stmt Statement to close.</a>
<span class="sourceLineNo">145</span><a id="line.145">     */</a>
<span class="sourceLineNo">146</span><a id="line.146">    public static void closeQuietly(final Statement stmt) {</a>
<span class="sourceLineNo">147</span><a id="line.147">        try {</a>
<span class="sourceLineNo">148</span><a id="line.148">            close(stmt);</a>
<span class="sourceLineNo">149</span><a id="line.149">        } catch (final SQLException e) { // NOPMD</a>
<span class="sourceLineNo">150</span><a id="line.150">            // quiet</a>
<span class="sourceLineNo">151</span><a id="line.151">        }</a>
<span class="sourceLineNo">152</span><a id="line.152">    }</a>
<span class="sourceLineNo">153</span><a id="line.153"></a>
<span class="sourceLineNo">154</span><a id="line.154">    /**</a>
<span class="sourceLineNo">155</span><a id="line.155">     * Commits a {@code Connection} then closes it, avoid closing if null.</a>
<span class="sourceLineNo">156</span><a id="line.156">     *</a>
<span class="sourceLineNo">157</span><a id="line.157">     * @param conn Connection to close.</a>
<span class="sourceLineNo">158</span><a id="line.158">     * @throws SQLException if a database access error occurs</a>
<span class="sourceLineNo">159</span><a id="line.159">     */</a>
<span class="sourceLineNo">160</span><a id="line.160">    public static void commitAndClose(final Connection conn) throws SQLException {</a>
<span class="sourceLineNo">161</span><a id="line.161">        if (conn != null) {</a>
<span class="sourceLineNo">162</span><a id="line.162">            try {</a>
<span class="sourceLineNo">163</span><a id="line.163">                conn.commit();</a>
<span class="sourceLineNo">164</span><a id="line.164">            } finally {</a>
<span class="sourceLineNo">165</span><a id="line.165">                conn.close();</a>
<span class="sourceLineNo">166</span><a id="line.166">            }</a>
<span class="sourceLineNo">167</span><a id="line.167">        }</a>
<span class="sourceLineNo">168</span><a id="line.168">    }</a>
<span class="sourceLineNo">169</span><a id="line.169"></a>
<span class="sourceLineNo">170</span><a id="line.170">    /**</a>
<span class="sourceLineNo">171</span><a id="line.171">     * Commits a {@code Connection} then closes it, avoid closing if null</a>
<span class="sourceLineNo">172</span><a id="line.172">     * and hide any SQLExceptions that occur.</a>
<span class="sourceLineNo">173</span><a id="line.173">     *</a>
<span class="sourceLineNo">174</span><a id="line.174">     * @param conn Connection to close.</a>
<span class="sourceLineNo">175</span><a id="line.175">     */</a>
<span class="sourceLineNo">176</span><a id="line.176">    public static void commitAndCloseQuietly(final Connection conn) {</a>
<span class="sourceLineNo">177</span><a id="line.177">        try {</a>
<span class="sourceLineNo">178</span><a id="line.178">            commitAndClose(conn);</a>
<span class="sourceLineNo">179</span><a id="line.179">        } catch (final SQLException e) { // NOPMD</a>
<span class="sourceLineNo">180</span><a id="line.180">            // quiet</a>
<span class="sourceLineNo">181</span><a id="line.181">        }</a>
<span class="sourceLineNo">182</span><a id="line.182">    }</a>
<span class="sourceLineNo">183</span><a id="line.183"></a>
<span class="sourceLineNo">184</span><a id="line.184">    /**</a>
<span class="sourceLineNo">185</span><a id="line.185">     * Loads and registers a database driver class.</a>
<span class="sourceLineNo">186</span><a id="line.186">     * If this succeeds, it returns true, else it returns false.</a>
<span class="sourceLineNo">187</span><a id="line.187">     *</a>
<span class="sourceLineNo">188</span><a id="line.188">     * @param driverClassName of driver to load</a>
<span class="sourceLineNo">189</span><a id="line.189">     * @return boolean {@code true} if the driver was found, otherwise {@code false}</a>
<span class="sourceLineNo">190</span><a id="line.190">     */</a>
<span class="sourceLineNo">191</span><a id="line.191">    public static boolean loadDriver(final String driverClassName) {</a>
<span class="sourceLineNo">192</span><a id="line.192">        return loadDriver(DbUtils.class.getClassLoader(), driverClassName);</a>
<span class="sourceLineNo">193</span><a id="line.193">    }</a>
<span class="sourceLineNo">194</span><a id="line.194"></a>
<span class="sourceLineNo">195</span><a id="line.195">    /**</a>
<span class="sourceLineNo">196</span><a id="line.196">     * Loads and registers a database driver class.</a>
<span class="sourceLineNo">197</span><a id="line.197">     * If this succeeds, it returns true, else it returns false.</a>
<span class="sourceLineNo">198</span><a id="line.198">     *</a>
<span class="sourceLineNo">199</span><a id="line.199">     * @param classLoader the class loader used to load the driver class</a>
<span class="sourceLineNo">200</span><a id="line.200">     * @param driverClassName of driver to load</a>
<span class="sourceLineNo">201</span><a id="line.201">     * @return boolean {@code true} if the driver was found, otherwise {@code false}</a>
<span class="sourceLineNo">202</span><a id="line.202">     * @since 1.4</a>
<span class="sourceLineNo">203</span><a id="line.203">     */</a>
<span class="sourceLineNo">204</span><a id="line.204">    public static boolean loadDriver(final ClassLoader classLoader, final String driverClassName) {</a>
<span class="sourceLineNo">205</span><a id="line.205">        try {</a>
<span class="sourceLineNo">206</span><a id="line.206">            final Class&lt;?&gt; loadedClass = classLoader.loadClass(driverClassName);</a>
<span class="sourceLineNo">207</span><a id="line.207"></a>
<span class="sourceLineNo">208</span><a id="line.208">            if (!Driver.class.isAssignableFrom(loadedClass)) {</a>
<span class="sourceLineNo">209</span><a id="line.209">                return false;</a>
<span class="sourceLineNo">210</span><a id="line.210">            }</a>
<span class="sourceLineNo">211</span><a id="line.211"></a>
<span class="sourceLineNo">212</span><a id="line.212">            @SuppressWarnings("unchecked") // guarded by previous check</a>
<span class="sourceLineNo">213</span><a id="line.213">            final</a>
<span class="sourceLineNo">214</span><a id="line.214">            Class&lt;Driver&gt; driverClass = (Class&lt;Driver&gt;) loadedClass;</a>
<span class="sourceLineNo">215</span><a id="line.215">            final Constructor&lt;Driver&gt; driverConstructor = driverClass.getConstructor();</a>
<span class="sourceLineNo">216</span><a id="line.216"></a>
<span class="sourceLineNo">217</span><a id="line.217">            // make Constructor accessible if it is private</a>
<span class="sourceLineNo">218</span><a id="line.218">            @SuppressWarnings("deprecation")</a>
<span class="sourceLineNo">219</span><a id="line.219">            // TODO This is deprecated in Java9 and canAccess() should be used. Adding suppression for building on</a>
<span class="sourceLineNo">220</span><a id="line.220">            //      later JDKs without a warning.</a>
<span class="sourceLineNo">221</span><a id="line.221">            final boolean isConstructorAccessible = driverConstructor.isAccessible();</a>
<span class="sourceLineNo">222</span><a id="line.222">            if (!isConstructorAccessible) {</a>
<span class="sourceLineNo">223</span><a id="line.223">                driverConstructor.setAccessible(true);</a>
<span class="sourceLineNo">224</span><a id="line.224">            }</a>
<span class="sourceLineNo">225</span><a id="line.225"></a>
<span class="sourceLineNo">226</span><a id="line.226">            try {</a>
<span class="sourceLineNo">227</span><a id="line.227">                final Driver driver = driverConstructor.newInstance();</a>
<span class="sourceLineNo">228</span><a id="line.228">                registerDriver(new DriverProxy(driver));</a>
<span class="sourceLineNo">229</span><a id="line.229">            } finally {</a>
<span class="sourceLineNo">230</span><a id="line.230">                driverConstructor.setAccessible(isConstructorAccessible);</a>
<span class="sourceLineNo">231</span><a id="line.231">            }</a>
<span class="sourceLineNo">232</span><a id="line.232"></a>
<span class="sourceLineNo">233</span><a id="line.233">            return true;</a>
<span class="sourceLineNo">234</span><a id="line.234">        } catch (final RuntimeException e) {</a>
<span class="sourceLineNo">235</span><a id="line.235">            return false;</a>
<span class="sourceLineNo">236</span><a id="line.236">        } catch (final Exception e) {</a>
<span class="sourceLineNo">237</span><a id="line.237">            return false;</a>
<span class="sourceLineNo">238</span><a id="line.238">        }</a>
<span class="sourceLineNo">239</span><a id="line.239">    }</a>
<span class="sourceLineNo">240</span><a id="line.240"></a>
<span class="sourceLineNo">241</span><a id="line.241">    /**</a>
<span class="sourceLineNo">242</span><a id="line.242">     * Print the stack trace for a SQLException to STDERR.</a>
<span class="sourceLineNo">243</span><a id="line.243">     *</a>
<span class="sourceLineNo">244</span><a id="line.244">     * @param e SQLException to print stack trace of</a>
<span class="sourceLineNo">245</span><a id="line.245">     */</a>
<span class="sourceLineNo">246</span><a id="line.246">    public static void printStackTrace(final SQLException e) {</a>
<span class="sourceLineNo">247</span><a id="line.247">        printStackTrace(e, new PrintWriter(System.err));</a>
<span class="sourceLineNo">248</span><a id="line.248">    }</a>
<span class="sourceLineNo">249</span><a id="line.249"></a>
<span class="sourceLineNo">250</span><a id="line.250">    /**</a>
<span class="sourceLineNo">251</span><a id="line.251">     * Print the stack trace for a SQLException to a</a>
<span class="sourceLineNo">252</span><a id="line.252">     * specified PrintWriter.</a>
<span class="sourceLineNo">253</span><a id="line.253">     *</a>
<span class="sourceLineNo">254</span><a id="line.254">     * @param e SQLException to print stack trace of</a>
<span class="sourceLineNo">255</span><a id="line.255">     * @param pw PrintWriter to print to</a>
<span class="sourceLineNo">256</span><a id="line.256">     */</a>
<span class="sourceLineNo">257</span><a id="line.257">    public static void printStackTrace(final SQLException e, final PrintWriter pw) {</a>
<span class="sourceLineNo">258</span><a id="line.258"></a>
<span class="sourceLineNo">259</span><a id="line.259">        SQLException next = e;</a>
<span class="sourceLineNo">260</span><a id="line.260">        while (next != null) {</a>
<span class="sourceLineNo">261</span><a id="line.261">            next.printStackTrace(pw);</a>
<span class="sourceLineNo">262</span><a id="line.262">            next = next.getNextException();</a>
<span class="sourceLineNo">263</span><a id="line.263">            if (next != null) {</a>
<span class="sourceLineNo">264</span><a id="line.264">                pw.println("Next SQLException:");</a>
<span class="sourceLineNo">265</span><a id="line.265">            }</a>
<span class="sourceLineNo">266</span><a id="line.266">        }</a>
<span class="sourceLineNo">267</span><a id="line.267">    }</a>
<span class="sourceLineNo">268</span><a id="line.268"></a>
<span class="sourceLineNo">269</span><a id="line.269">    /**</a>
<span class="sourceLineNo">270</span><a id="line.270">     * Print warnings on a Connection to STDERR.</a>
<span class="sourceLineNo">271</span><a id="line.271">     *</a>
<span class="sourceLineNo">272</span><a id="line.272">     * @param conn Connection to print warnings from</a>
<span class="sourceLineNo">273</span><a id="line.273">     */</a>
<span class="sourceLineNo">274</span><a id="line.274">    public static void printWarnings(final Connection conn) {</a>
<span class="sourceLineNo">275</span><a id="line.275">        printWarnings(conn, new PrintWriter(System.err));</a>
<span class="sourceLineNo">276</span><a id="line.276">    }</a>
<span class="sourceLineNo">277</span><a id="line.277"></a>
<span class="sourceLineNo">278</span><a id="line.278">    /**</a>
<span class="sourceLineNo">279</span><a id="line.279">     * Print warnings on a Connection to a specified PrintWriter.</a>
<span class="sourceLineNo">280</span><a id="line.280">     *</a>
<span class="sourceLineNo">281</span><a id="line.281">     * @param conn Connection to print warnings from</a>
<span class="sourceLineNo">282</span><a id="line.282">     * @param pw PrintWriter to print to</a>
<span class="sourceLineNo">283</span><a id="line.283">     */</a>
<span class="sourceLineNo">284</span><a id="line.284">    public static void printWarnings(final Connection conn, final PrintWriter pw) {</a>
<span class="sourceLineNo">285</span><a id="line.285">        if (conn != null) {</a>
<span class="sourceLineNo">286</span><a id="line.286">            try {</a>
<span class="sourceLineNo">287</span><a id="line.287">                printStackTrace(conn.getWarnings(), pw);</a>
<span class="sourceLineNo">288</span><a id="line.288">            } catch (final SQLException e) {</a>
<span class="sourceLineNo">289</span><a id="line.289">                printStackTrace(e, pw);</a>
<span class="sourceLineNo">290</span><a id="line.290">            }</a>
<span class="sourceLineNo">291</span><a id="line.291">        }</a>
<span class="sourceLineNo">292</span><a id="line.292">    }</a>
<span class="sourceLineNo">293</span><a id="line.293"></a>
<span class="sourceLineNo">294</span><a id="line.294">    /**</a>
<span class="sourceLineNo">295</span><a id="line.295">     * Rollback any changes made on the given connection.</a>
<span class="sourceLineNo">296</span><a id="line.296">     * @param conn Connection to rollback.  A null value is legal.</a>
<span class="sourceLineNo">297</span><a id="line.297">     * @throws SQLException if a database access error occurs</a>
<span class="sourceLineNo">298</span><a id="line.298">     */</a>
<span class="sourceLineNo">299</span><a id="line.299">    public static void rollback(final Connection conn) throws SQLException {</a>
<span class="sourceLineNo">300</span><a id="line.300">        if (conn != null) {</a>
<span class="sourceLineNo">301</span><a id="line.301">            conn.rollback();</a>
<span class="sourceLineNo">302</span><a id="line.302">        }</a>
<span class="sourceLineNo">303</span><a id="line.303">    }</a>
<span class="sourceLineNo">304</span><a id="line.304"></a>
<span class="sourceLineNo">305</span><a id="line.305">    /**</a>
<span class="sourceLineNo">306</span><a id="line.306">     * Performs a rollback on the {@code Connection} then closes it,</a>
<span class="sourceLineNo">307</span><a id="line.307">     * avoid closing if null.</a>
<span class="sourceLineNo">308</span><a id="line.308">     *</a>
<span class="sourceLineNo">309</span><a id="line.309">     * @param conn Connection to rollback.  A null value is legal.</a>
<span class="sourceLineNo">310</span><a id="line.310">     * @throws SQLException if a database access error occurs</a>
<span class="sourceLineNo">311</span><a id="line.311">     * @since DbUtils 1.1</a>
<span class="sourceLineNo">312</span><a id="line.312">     */</a>
<span class="sourceLineNo">313</span><a id="line.313">    public static void rollbackAndClose(final Connection conn) throws SQLException {</a>
<span class="sourceLineNo">314</span><a id="line.314">        if (conn != null) {</a>
<span class="sourceLineNo">315</span><a id="line.315">            try {</a>
<span class="sourceLineNo">316</span><a id="line.316">                conn.rollback();</a>
<span class="sourceLineNo">317</span><a id="line.317">            } finally {</a>
<span class="sourceLineNo">318</span><a id="line.318">                conn.close();</a>
<span class="sourceLineNo">319</span><a id="line.319">            }</a>
<span class="sourceLineNo">320</span><a id="line.320">        }</a>
<span class="sourceLineNo">321</span><a id="line.321">    }</a>
<span class="sourceLineNo">322</span><a id="line.322"></a>
<span class="sourceLineNo">323</span><a id="line.323">    /**</a>
<span class="sourceLineNo">324</span><a id="line.324">     * Performs a rollback on the {@code Connection} then closes it,</a>
<span class="sourceLineNo">325</span><a id="line.325">     * avoid closing if null and hide any SQLExceptions that occur.</a>
<span class="sourceLineNo">326</span><a id="line.326">     *</a>
<span class="sourceLineNo">327</span><a id="line.327">     * @param conn Connection to rollback.  A null value is legal.</a>
<span class="sourceLineNo">328</span><a id="line.328">     * @since DbUtils 1.1</a>
<span class="sourceLineNo">329</span><a id="line.329">     */</a>
<span class="sourceLineNo">330</span><a id="line.330">    public static void rollbackAndCloseQuietly(final Connection conn) {</a>
<span class="sourceLineNo">331</span><a id="line.331">        try {</a>
<span class="sourceLineNo">332</span><a id="line.332">            rollbackAndClose(conn);</a>
<span class="sourceLineNo">333</span><a id="line.333">        } catch (final SQLException e) { // NOPMD</a>
<span class="sourceLineNo">334</span><a id="line.334">            // quiet</a>
<span class="sourceLineNo">335</span><a id="line.335">        }</a>
<span class="sourceLineNo">336</span><a id="line.336">    }</a>
<span class="sourceLineNo">337</span><a id="line.337"></a>
<span class="sourceLineNo">338</span><a id="line.338">    /**</a>
<span class="sourceLineNo">339</span><a id="line.339">     * Simple {@link Driver} proxy class that proxies a JDBC Driver loaded dynamically.</a>
<span class="sourceLineNo">340</span><a id="line.340">     *</a>
<span class="sourceLineNo">341</span><a id="line.341">     * @since 1.6</a>
<span class="sourceLineNo">342</span><a id="line.342">     */</a>
<span class="sourceLineNo">343</span><a id="line.343">    static final class DriverProxy implements Driver {</a>
<span class="sourceLineNo">344</span><a id="line.344"></a>
<span class="sourceLineNo">345</span><a id="line.345">        private boolean parentLoggerSupported = true;</a>
<span class="sourceLineNo">346</span><a id="line.346"></a>
<span class="sourceLineNo">347</span><a id="line.347">        /**</a>
<span class="sourceLineNo">348</span><a id="line.348">         * The adapted JDBC Driver loaded dynamically.</a>
<span class="sourceLineNo">349</span><a id="line.349">         */</a>
<span class="sourceLineNo">350</span><a id="line.350">        private final Driver adapted;</a>
<span class="sourceLineNo">351</span><a id="line.351"></a>
<span class="sourceLineNo">352</span><a id="line.352">        /**</a>
<span class="sourceLineNo">353</span><a id="line.353">         * Creates a new JDBC Driver that adapts a JDBC Driver loaded dynamically.</a>
<span class="sourceLineNo">354</span><a id="line.354">         *</a>
<span class="sourceLineNo">355</span><a id="line.355">         * @param adapted the adapted JDBC Driver loaded dynamically.</a>
<span class="sourceLineNo">356</span><a id="line.356">         */</a>
<span class="sourceLineNo">357</span><a id="line.357">        public DriverProxy(final Driver adapted) {</a>
<span class="sourceLineNo">358</span><a id="line.358">            this.adapted = adapted;</a>
<span class="sourceLineNo">359</span><a id="line.359">        }</a>
<span class="sourceLineNo">360</span><a id="line.360"></a>
<span class="sourceLineNo">361</span><a id="line.361">        /**</a>
<span class="sourceLineNo">362</span><a id="line.362">         * {@inheritDoc}</a>
<span class="sourceLineNo">363</span><a id="line.363">         */</a>
<span class="sourceLineNo">364</span><a id="line.364">        @Override</a>
<span class="sourceLineNo">365</span><a id="line.365">        public boolean acceptsURL(final String url) throws SQLException {</a>
<span class="sourceLineNo">366</span><a id="line.366">            return adapted.acceptsURL(url);</a>
<span class="sourceLineNo">367</span><a id="line.367">        }</a>
<span class="sourceLineNo">368</span><a id="line.368"></a>
<span class="sourceLineNo">369</span><a id="line.369">        /**</a>
<span class="sourceLineNo">370</span><a id="line.370">         * {@inheritDoc}</a>
<span class="sourceLineNo">371</span><a id="line.371">         */</a>
<span class="sourceLineNo">372</span><a id="line.372">        @Override</a>
<span class="sourceLineNo">373</span><a id="line.373">        public Connection connect(final String url, final Properties info) throws SQLException {</a>
<span class="sourceLineNo">374</span><a id="line.374">            return adapted.connect(url, info);</a>
<span class="sourceLineNo">375</span><a id="line.375">        }</a>
<span class="sourceLineNo">376</span><a id="line.376"></a>
<span class="sourceLineNo">377</span><a id="line.377">        /**</a>
<span class="sourceLineNo">378</span><a id="line.378">         * {@inheritDoc}</a>
<span class="sourceLineNo">379</span><a id="line.379">         */</a>
<span class="sourceLineNo">380</span><a id="line.380">        @Override</a>
<span class="sourceLineNo">381</span><a id="line.381">        public int getMajorVersion() {</a>
<span class="sourceLineNo">382</span><a id="line.382">            return adapted.getMajorVersion();</a>
<span class="sourceLineNo">383</span><a id="line.383">        }</a>
<span class="sourceLineNo">384</span><a id="line.384"></a>
<span class="sourceLineNo">385</span><a id="line.385">        /**</a>
<span class="sourceLineNo">386</span><a id="line.386">         * {@inheritDoc}</a>
<span class="sourceLineNo">387</span><a id="line.387">         */</a>
<span class="sourceLineNo">388</span><a id="line.388">        @Override</a>
<span class="sourceLineNo">389</span><a id="line.389">        public int getMinorVersion() {</a>
<span class="sourceLineNo">390</span><a id="line.390">            return adapted.getMinorVersion();</a>
<span class="sourceLineNo">391</span><a id="line.391">        }</a>
<span class="sourceLineNo">392</span><a id="line.392"></a>
<span class="sourceLineNo">393</span><a id="line.393">        /**</a>
<span class="sourceLineNo">394</span><a id="line.394">         * {@inheritDoc}</a>
<span class="sourceLineNo">395</span><a id="line.395">         */</a>
<span class="sourceLineNo">396</span><a id="line.396">        @Override</a>
<span class="sourceLineNo">397</span><a id="line.397">        public DriverPropertyInfo[] getPropertyInfo(final String url, final Properties info) throws SQLException {</a>
<span class="sourceLineNo">398</span><a id="line.398">            return adapted.getPropertyInfo(url, info);</a>
<span class="sourceLineNo">399</span><a id="line.399">        }</a>
<span class="sourceLineNo">400</span><a id="line.400"></a>
<span class="sourceLineNo">401</span><a id="line.401">        /**</a>
<span class="sourceLineNo">402</span><a id="line.402">         * {@inheritDoc}</a>
<span class="sourceLineNo">403</span><a id="line.403">         */</a>
<span class="sourceLineNo">404</span><a id="line.404">        @Override</a>
<span class="sourceLineNo">405</span><a id="line.405">        public boolean jdbcCompliant() {</a>
<span class="sourceLineNo">406</span><a id="line.406">            return adapted.jdbcCompliant();</a>
<span class="sourceLineNo">407</span><a id="line.407">        }</a>
<span class="sourceLineNo">408</span><a id="line.408"></a>
<span class="sourceLineNo">409</span><a id="line.409">        /**</a>
<span class="sourceLineNo">410</span><a id="line.410">         * Java 1.7 method.</a>
<span class="sourceLineNo">411</span><a id="line.411">         */</a>
<span class="sourceLineNo">412</span><a id="line.412">        @Override</a>
<span class="sourceLineNo">413</span><a id="line.413">        public Logger getParentLogger() throws SQLFeatureNotSupportedException {</a>
<span class="sourceLineNo">414</span><a id="line.414">            if (parentLoggerSupported) {</a>
<span class="sourceLineNo">415</span><a id="line.415">                try {</a>
<span class="sourceLineNo">416</span><a id="line.416">                    final Method method = adapted.getClass().getMethod("getParentLogger");</a>
<span class="sourceLineNo">417</span><a id="line.417">                    return (Logger)method.invoke(adapted);</a>
<span class="sourceLineNo">418</span><a id="line.418">                } catch (final NoSuchMethodException e) {</a>
<span class="sourceLineNo">419</span><a id="line.419">                    parentLoggerSupported = false;</a>
<span class="sourceLineNo">420</span><a id="line.420">                    throw new SQLFeatureNotSupportedException(e);</a>
<span class="sourceLineNo">421</span><a id="line.421">                } catch (final IllegalAccessException e) {</a>
<span class="sourceLineNo">422</span><a id="line.422">                    parentLoggerSupported = false;</a>
<span class="sourceLineNo">423</span><a id="line.423">                    throw new SQLFeatureNotSupportedException(e);</a>
<span class="sourceLineNo">424</span><a id="line.424">                } catch (final InvocationTargetException e) {</a>
<span class="sourceLineNo">425</span><a id="line.425">                    parentLoggerSupported = false;</a>
<span class="sourceLineNo">426</span><a id="line.426">                    throw new SQLFeatureNotSupportedException(e);</a>
<span class="sourceLineNo">427</span><a id="line.427">                }</a>
<span class="sourceLineNo">428</span><a id="line.428">            }</a>
<span class="sourceLineNo">429</span><a id="line.429">            throw new SQLFeatureNotSupportedException();</a>
<span class="sourceLineNo">430</span><a id="line.430">        }</a>
<span class="sourceLineNo">431</span><a id="line.431"></a>
<span class="sourceLineNo">432</span><a id="line.432">    }</a>
<span class="sourceLineNo">433</span><a id="line.433"></a>
<span class="sourceLineNo">434</span><a id="line.434">}</a>




























































</pre>
</div>
</main>
</body>
</html>
